package com.mySampleApplication.server;

import java.util.concurrent.atomic.AtomicLong;

public class SerialNumberGenerators {
    private static AtomicLong serialNumber = new AtomicLong(0);
    private static final int DEFAULT_LENGTH = 6;
    private static final String DEFAULT_PREFIX = "";
    private static final int DEFAULT_STEP = 1;
    public static void initializeSerialNumber(long maxSerialNumber) {
        serialNumber.set(maxSerialNumber);
    }
    public static String getNextSerialNumber() {
        return getNextSerialNumber(DEFAULT_LENGTH, DEFAULT_PREFIX, DEFAULT_STEP);
    }

    public static String getNextSerialNumber(int length) {
        return getNextSerialNumber(length, DEFAULT_PREFIX, DEFAULT_STEP);
    }

    public static String getNextSerialNumber(String prefix) {
        return getNextSerialNumber(DEFAULT_LENGTH, prefix, DEFAULT_STEP);
    }

    public static String getNextSerialNumber(int length, String prefix) {
        return getNextSerialNumber(length, prefix, DEFAULT_STEP);
    }

    public static String getNextSerialNumber(int length, String prefix, int step) {
        long nextSerialNumber = serialNumber.getAndAdd(step);
        String serialNumberString = String.valueOf(nextSerialNumber);
        int paddingLength = length - prefix.length() - serialNumberString.length();
        if (paddingLength < 0) {
            throw new RuntimeException("Serial number length is too short.");
        }
        String padding = "";
        for (int i = 0; i < paddingLength; i++) {
            padding += "0";
        }
        return prefix + padding + serialNumberString;
    }
}
